package apresentacao;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JPasswordField;


import persistencia.Conexao;
import persistencia.DaoCliente;
import dominio.Cliente;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;

public class TelaCadastroCliente extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Nome;
	private JTextField textField_Telefone;
	private JTextField textField_Email;
	private JTextField textField_Usuario;
	private JPasswordField textField_Senha;
	private JPasswordField textField_5;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			TelaCadastroCliente dialog = new TelaCadastroCliente();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public TelaCadastroCliente() {
		setBounds(100, 100, 483, 554);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(240, 230, 140));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lbl_cadUsuario = new JLabel("Cadastro - Usu\u00E1rio");
			lbl_cadUsuario.setHorizontalAlignment(SwingConstants.CENTER);
			lbl_cadUsuario.setFont(new Font("Arial", Font.BOLD, 20));
			lbl_cadUsuario.setBounds(10, 10, 447, 38);
			contentPanel.add(lbl_cadUsuario);
		}
		{
			JPanel panel = new JPanel();
			panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel.setBackground(new Color(238, 232, 170));
			panel.setBounds(43, 59, 383, 404);
			contentPanel.add(panel);
			panel.setLayout(null);
			{
				JLabel lbl_nome = new JLabel("Nome :");
				lbl_nome.setFont(new Font("Arial", Font.BOLD, 13));
				lbl_nome.setBounds(44, 11, 53, 20);
				panel.add(lbl_nome);
			}
			{
				textField_Nome = new JTextField();
				textField_Nome.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_Nome.setBounds(44, 32, 297, 27);
				panel.add(textField_Nome);
				textField_Nome.setColumns(10);
			}
			{
				JLabel lbl_telefone = new JLabel("Telefone :");
				lbl_telefone.setFont(new Font("Arial", Font.BOLD, 13));
				lbl_telefone.setBounds(44, 74, 69, 20);
				panel.add(lbl_telefone);
			}
			{
				textField_Telefone = new JTextField();
				textField_Telefone.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_Telefone.setBounds(44, 94, 175, 27);
				panel.add(textField_Telefone);
				textField_Telefone.setColumns(10);
			}
			{
				JLabel lbl_email = new JLabel("Email :");
				lbl_email.setFont(new Font("Arial", Font.BOLD, 13));
				lbl_email.setBounds(44, 142, 46, 20);
				panel.add(lbl_email);
			}
			{
				textField_Email = new JTextField();
				textField_Email.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_Email.setBounds(44, 162, 175, 27);
				panel.add(textField_Email);
				textField_Email.setColumns(10);
			}
			{
				textField_Usuario = new JTextField();
				textField_Usuario.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_Usuario.setBounds(44, 240, 148, 27);
				panel.add(textField_Usuario);
				textField_Usuario.setColumns(10);
			}
			{
				JLabel lbl_usuario = new JLabel("Usu\u00E1rio :");
				lbl_usuario.setBounds(44, 220, 63, 20);
				panel.add(lbl_usuario);
				lbl_usuario.setFont(new Font("Arial", Font.BOLD, 13));
			}
			{
				JLabel lbl_senha = new JLabel("Senha :");
				lbl_senha.setBounds(44, 280, 63, 20);
				panel.add(lbl_senha);
				lbl_senha.setFont(new Font("Arial", Font.BOLD, 13));
			}
			{
				textField_Senha = new JPasswordField();
				textField_Senha.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_Senha.setBounds(44, 299, 148, 27);
				panel.add(textField_Senha);
				textField_Senha.setColumns(10);
			}
			{
				JLabel lblConfirmarSenha = new JLabel("Confirmar Senha :");
				lblConfirmarSenha.setBounds(44, 339, 131, 20);
				panel.add(lblConfirmarSenha);
				lblConfirmarSenha.setFont(new Font("Arial", Font.BOLD, 13));
			}
			{
				textField_5 = new JPasswordField();
				textField_5.setFont(new Font("Arial", Font.PLAIN, 12));
				textField_5.setBounds(44, 359, 148, 27);
				panel.add(textField_5);
				textField_5.setColumns(10);
			}
			
			JSeparator separator = new JSeparator();
			separator.setBounds(27, 207, 325, 2);
			panel.add(separator);
		}
		
		JButton btn_Salvar = new JButton("Salvar");
		btn_Salvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Cliente cliente = new Cliente();
				Connection conn = null;
				try
				{
					Conexao bd = new Conexao();
					DaoCliente dao = new DaoCliente();
					conn = bd.obtemConexao();
				
					conn.setAutoCommit(false);
				
					
					cliente.setNome(textField_Nome.getText());
					
					
					String CampoSenha = new String(textField_Senha.getPassword());
									
					cliente.setSenha(CampoSenha);
					
					
					
					cliente.setEmail(textField_Email.getText());
					
					cliente.setLogin(textField_Usuario.getText());
					
					cliente.setIdtipopessoa(1);
					
				
					Integer x = Integer.valueOf( textField_Telefone.getText() );
					
					long n = Long.valueOf(x);

					cliente.setTelefone(n);
					
					
					dao.incluir(conn, cliente);
					conn.commit();
					JOptionPane.showMessageDialog(rootPane, "Cliente cadastrado com sucesso!");
					
					
			        textField_Nome.setText("");
			        textField_Senha.setText("");
			        textField_Email.setText("");
			        textField_Usuario.setText("");
			        textField_5.setText("");
			        textField_Telefone.setText("");
					
					
				}
				catch (Exception ex) {
					ex.printStackTrace();
					if (conn != null) {
						try {
							conn.rollback();
						}
						catch (SQLException e1) {
							System.out.print(e1.getStackTrace());
						}
					}
				}
				finally {
					if (conn != null) {
						try {
							conn.close();
						}
						catch (SQLException e1) {
							System.out.print(e1.getStackTrace());
						}
					}
				}
			}
		});	
				
		
		btn_Salvar.setFont(new Font("Arial", Font.BOLD, 14));
		btn_Salvar.setBounds(268, 474, 83, 33);
		contentPanel.add(btn_Salvar);
		
		JButton btn_Voltar = new JButton("Voltar");
		btn_Voltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				TelaPrincipal telaPrincipal = new TelaPrincipal();
				telaPrincipal.frame.setVisible(true);
				
			}
		});
		btn_Voltar.setFont(new Font("Arial", Font.PLAIN, 12));
		btn_Voltar.setBounds(356, 474, 70, 33);
		contentPanel.add(btn_Voltar);
	}
}
